package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.*;
import com.dgut.doctorservice.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/doctorservice/registration")
@CrossOrigin
public class RegistrationController {

    @Autowired
    RegistrationService registrationService;
    @Autowired
    PatientInfoService patientInfoService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    ScheduleService scheduleService;
    @Autowired
    PrescriptionService prescriptionService;
    @Autowired
    MedecineInfoService medecineInfoService;

    @ApiOperation("病人在前台进行线下挂号病人信息填写")
    @PostMapping("addPatientByReception")
    public Result addPatientByReception(@RequestBody PatientInfo patientInfo){
        //patientInfoService.save(patientInfo);
        QueryWrapper<PatientInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",patientInfo.getPhone());
        PatientInfo one = patientInfoService.getOne(wrapper);
        if(one!=null){
            String id=one.getId();
            return Result.succ().data("id",id).message("该手机号已存在！");
        }else{
            patientInfoService.save(patientInfo);
            QueryWrapper<PatientInfo> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("phone",patientInfo.getPhone());
            PatientInfo one1 = patientInfoService.getOne(wrapper1);
            String id=one1.getId();
            return Result.succ().data("id",id).message("添加成功！");
        }

    }


    @ApiOperation("病人进行挂号")
    @PostMapping("addRegistration")
    public Result addRegistration(@RequestBody Registration registration){
        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
        wrapper.eq("date",registration.getDate());
        wrapper.eq("d_id",registration.getDId());
        Schedule one = scheduleService.getOne(wrapper);
        if (one!=null){
            if (one.getNumber()!=0){
                one.setNumber(one.getNumber()-1);
                registrationService.save(registration);
                scheduleService.updateById(one);
                return Result.succ().message("挂号成功！");
            }else {
                return Result.succ().message("挂号失败，已无号！");
            }
        }else {
            return Result.succ().message("没有该医生的号了");
        }
    }



    @ApiOperation("医生获取病人挂号列表")
    @PostMapping("getRegistrationByDId")
    public Result getRegistration(@RequestParam String id){
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("d_id",id);
        List<Registration> list = registrationService.list(wrapper);
        List<PatientInfo> patients=new ArrayList<>();
        //此处进行挂号列表的判断
        if(list!=null){
            for (int i = 0; i < list.size(); i++) {
                PatientInfo byId = patientInfoService.getById(list.get(i).getPId());
                patients.add(byId);
            }
            return Result.succ().data("list",list).data("patients",patients);
        }else {
            return Result.succ().message("病人挂号列表为空!");
        }


    }

    @ApiOperation("根据病人id获取挂号记录")
    @PostMapping("getRegistrationByPId")
    public Result getRegistrationByPId(@RequestParam String id){
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("p_id",id);
        List<Registration> list = registrationService.list(wrapper);
        List<Doctor> doctors=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Doctor byId = doctorService.getById(list.get(i).getDId());
            doctors.add(byId);
        }
        return Result.succ().data("list",list).data("doctors",doctors);
    }


    @ApiOperation("台账功能实现及获取今日挂号人数以及诊断人数")
    @GetMapping("getNumbers")
    public Result getNumbers(@RequestParam String dateString){
//        Date date=new Date();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String dateString=sdf.format(date);
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("date",dateString);

        //System.out.println(count);
        QueryWrapper<Registration> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("date",dateString);
        wrapper1.eq("flags",1);


        QueryWrapper<Registration> wrapper11 = new QueryWrapper<>();
        wrapper11.eq("flags",1);

        List<Registration> list2 = registrationService.list(wrapper);
        int count = list2.size();

        List<Registration> list = registrationService.list(wrapper1);
        int count1 =list.size();

        List<Registration> list11 = registrationService.list(wrapper11);


        double registrationIn=0;
        double allRegistrationIn=0;
        for (int i = 0; i <list2.size() ; i++) {
            QueryWrapper<Doctor> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("id",list2.get(i).getDId());
            Doctor one = doctorService.getOne(wrapper2);
            if (one.getLevel()==1){
                registrationIn+=30;
            }else {
                registrationIn+=15;
            }
        }

        List<Registration> list3 = registrationService.list();
        for (int i = 0; i <list3.size() ; i++) {
            QueryWrapper<Doctor> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("id",list3.get(i).getDId());
            Doctor one = doctorService.getOne(wrapper2);
            if (one.getLevel()==1){
                allRegistrationIn+=30;
            }else {
                allRegistrationIn+=15;
            }
        }
        double medecineOut=0;
        double allMedecineOut=0;
        double medecineIn=0;
        double allMedecineIn=0;
        double medecineGet=0;
        double allMedecineGet=0;


        QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flags",1);
        queryWrapper.eq("date",dateString);

        List<Prescription> list1 = prescriptionService.list(queryWrapper);
        QueryWrapper<Prescription> queryWrapper11 = new QueryWrapper<>();
        queryWrapper11.eq("flags",1);
        List<Prescription> list111 = prescriptionService.list(queryWrapper11);
        for (int i = 0; i <list1.size() ; i++) {
            QueryWrapper<MedecineInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",list111.get(i).getMId());
            medecineOut+=medecineInfoService.getOne(queryWrapper1).getPrice();
            medecineIn+=medecineInfoService.getOne(queryWrapper1).getSell();
        }
        for (int i = 0; i <list111.size() ; i++) {
            QueryWrapper<MedecineInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",list111.get(i).getMId());
            allMedecineOut+=medecineInfoService.getOne(queryWrapper1).getPrice();
            allMedecineIn+=medecineInfoService.getOne(queryWrapper1).getSell();
        }
        allMedecineGet=allMedecineIn-allMedecineOut;

        List today=new ArrayList();
        today.add(registrationIn);
        today.add(medecineOut);
        today.add(medecineIn);
        today.add(medecineGet);

        List all=new ArrayList();
        all.add(allRegistrationIn);
        all.add(allMedecineOut);
        all.add(allMedecineIn);
        all.add(allMedecineGet);


        //System.out.println(count1);
        //System.out.println(registrationService.count());
        return Result.succ().data("total",count).data("finish",count1).data("today",today).data("all",all);
    }

}

